package gallery
{
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.MouseEvent;

    import org.kolonitsky.alexey.events.ListEvent;

    import caurina.transitions.Tweener;



    /**
     *  Dispatched when user select item from list. This event carry
     *  selected item index.
     */
    [Event(name="select", type="org.kolonitsky.alexey.events.ListEvent")]

    

    public class SlotItem extends Sprite
    {

        public var index:int = 0;

        public function get image():DisplayObject
        {
            if (numChildren > 0)
                return getChildAt(0);
            return null;
        }
        
        public function SlotItem(index:int, defaultWidth:Number=50, defaultHeight:Number=50)
        {
            this.defaultWidth = defaultWidth;
            this.defaultHeight = defaultHeight;
            
	    useHandCursor = true;
	    buttonMode = true;
            mouseChildren = false;
	    addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
	    addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
	    addEventListener(MouseEvent.CLICK, clickHandler);
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }


        public function updateItem(image:DisplayObject):void
        {
            while (numChildren)
               removeChildAt(0);

            var scale:Number = image.width < defaultWidth ? image.width / defaultWidth : 1;
            image.scaleX = scale;
            image.scaleY = scale;

            var mc:Sprite = new Sprite;
            mc.x = scale * image.width / 2;
            mc.y = scale * image.height / 2;
            
	    image.x = -scale * image.width / 2;
	    image.y = -scale * image.height / 2;
	    mc.addChild(image);

            addChild(mc)

            draw();
        }

        


        //----------------------------------------------------------------------
        //
        //  Private logic
        //
        //----------------------------------------------------------------------

        private var defaultWidth:Number;
        private var defaultHeight:Number;

        private function draw():void
        {
            var w:Number = image ? image.width : defaultWidth;
            var h:Number = image ? image.height : defaultHeight;

            graphics.clear();
            graphics.beginFill(0xFF0000);
            graphics.drawRect(0, 0, w, h);
        }
        

        //----------------------------------------------------------------------
        //  Event handlers
        //----------------------------------------------------------------------

        private function addedToStageHandler(event:Event):void
        {
            draw();
        }
            

	private function mouseOverHandler(event:MouseEvent):void
	{
	    Tweener.addTween(image, {scaleX:0.8, scaleY:0.8, time:0.7});
	}

	private function mouseOutHandler(event:MouseEvent):void
	{
	    Tweener.addTween(image, {scaleX:1.0, scaleY:1.0, time:0.7});
	}

	private function clickHandler(event:MouseEvent):void
	{
	    var type:String = ListEvent.SELECT;
	    dispatchEvent(new ListEvent(type, index, true));
	}

    }
}